package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.missfood.R;
import com.qianfeng.missfood.utils.DataUtils;
import com.qianfeng.missfood.utils.SmsUtils;


public class SmsActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView getCode;
    private Handler handler;
    private String phone;
    private EditText securityCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        Intent getInte = getIntent();
        phone = getInte.getStringExtra("phone");

        ImageView pressBack = (ImageView) findViewById(R.id.pressBack);
        pressBack.setOnClickListener(this);
        securityCode = (EditText) findViewById(R.id.securityCode);


        getCode = (TextView) findViewById(R.id.getCode);
        getCode.setOnClickListener(this);
        Button register = (Button) findViewById(R.id.registerNext);
        register.setOnClickListener(this);
        SmsUtils.sendSms(this);

        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {


                int i = msg.arg1;
                if (i == 0) {

                    getCode.setText("重新获取");
                    getCode.setTextColor(getResources().getColor(R.color.blue));
                    getCode.setClickable(true);

                } else {

                    getCode.setText("重新获取(" + i + ")");
                }


            }
        };

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.pressBack:

                onBackPressed();
                break;
            case R.id.getCode:

                SmsUtils.sendSms(this);
                getCode.setClickable(false);
                getCode.setTextColor(getResources().getColor(R.color.silver));

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        for (int i = 29; i >= 0; i--) {

                            Message msg = handler.obtainMessage();
                            msg.arg1 = i;
                            handler.sendMessage(msg);
                            SystemClock.sleep(100);
                        }

                    }
                }).start();

                break;

            case R.id.registerNext:

                String code = securityCode.getText().toString().trim();
                String atCode = DataUtils.getSecurityCode() + "";
                if (!"".equals(code) && atCode.equals(code)) {

                    Intent intent = new Intent(this, UserMsgActivity.class);
                    intent.putExtra("phone", phone);
                    startActivity(intent);
                    break;

                } else {

                    Toast.makeText(this, "验证码输入错误", Toast.LENGTH_SHORT).show();
                }

        }

    }

}
